<template>
<div class="stock-wrapper ui pc marginTop10">
	<h3>规格ID：{{option.data.product_id}}</h3>
    <div class="date-model">
        <label><input type="radio"  checked value="1" class="hidden" v-model="whProDetail.is_price_date">
                日历模式</label>
				<!-- <label><input type="radio" v-model="whProDetail.is_price_date" value="0" class="hidden" :disabled='proDetail.is_quantity_date==1'> -->
        <label><input type="radio" v-model="whProDetail.is_price_date" value="0" class="hidden" >
                单一模式</label>
    </div>

    <div class="ui table-warpper"  >
        <table class="ui celled padded table" v-show="this.proDetail.is_quantity_date==0||componentName==='dateStock'">
            <tbody>
                <!-- <tr>
                    <td>成本价(￥)</td>
                    <td>{{proDetail.price}}</td>
                </tr> -->
                <tr v-show='this.proDetail.is_quantity_date==0'>
                    <td >库存(份)</td>
                    <td>{{proDetail.quantity}}</td>
                </tr>
				<tr v-show="componentName==='dateStock'">
					<td>最低售价</td>
					<td>
						<div class="fields inline">
		                    <div class="ui field input">
		                        <input type="number" v-model='bottom_price'>
		                    </div>
						</div>
					</td>
				</tr>
            </tbody>
        </table>
    </div>


	<singleStock v-show="componentName==='singleStock'" :showValid='true' :dateShowContent='dateShowContent' :disable="false" @upStockData='updateStockData' :productData='productData'></singleStock>
	<dateStock v-show="componentName==='dateStock'"  :dateShowContent='dateShowContent' :disable="false" @upStockData='updateStockData' :productData='productData'></dateStock>

	<div class="center-box marginTop10">
		<button class="ui primary button" @click.prevent.stop="sure">确认</button>
		<button class="ui button" @click.prevent.stop="hideMypop">取消</button>
	</div>
</div>
</template>
<script>
import tool from 'src/util/tool';
import util from '_util/index';
import logic from '_logic';

//子组件
import singleStock from '../stock/singleStock.vue'
import dateStock from '../stock/dateStock.vue'

export default {
    components: {
        singleStock,
        dateStock
    },
    data() {
        return {
            val: null,
            attrs: [],
            units: [],
            proDetail: {
                id: '',
                name: '',
                price: '',
                is_quantity_date: 0,
                quantity: 0
            },
			whProDetail:{
				is_price_date:0,
				sale_price:0,
				fc_serv:0,
				fc_serv_up:0,
				fc_sp:0
			},
			bottom_price:0,
			priceCalendar:[],
			quantityCalender:[],
			submitDateData:{
				bottom_price:0,
				list:[],
			},
			submitSingleData:[],
			quantityOption:[
				// {
                //     name: 'price',
                //     label: '成本价(￥)',
                //     disabled: true
                // },
				{
	                    name: 'quantity',
	                    label: '库存(份)',
	                    disabled: true
	                }],
            dateShowOption: [{
                    name: 'sale_price',
                    label: '售价(￥)'
                },
                {
                    name: 'fc_serv',
                    label: '服务者分成'
                },
                {
                    name: 'fc_serv_up',
                    label: '服务商分成'
                },
                {
                    name: 'fc_sp',
                    label: '供应商分成',
                }
            ]
        }
    },
    watch: {},
    methods: {
		checkInput(){
		  //检查输入
		  let arrayBool = true;
		  let data = this.productSubmitData;
		  if(Object.keys(data).indexOf('sale_price')>-1){
			  let fc_serv = data.fc_serv||0,
			  fc_serv_up = data.fc_serv_up||0,
			  fc_sp = data.fc_sp||0,
			  sale_price = data.sale_price||0;
			  if((fc_serv*1+fc_serv_up*1+fc_sp*1)>sale_price){
				  arrayBool = false;
				  util.showMsg(this, 'error', `分成不得大于售价`);
			  }
		  }
		  if(this.showValid&&Object.keys(data).indexOf('delayed_sale_price')>-1){
			  let delayed_fc_serv = data.delayed_fc_serv||0,
			  delayed_fc_serv_up = data.delayed_fc_serv_up||0,
			  delayed_fc_sp = data.delayed_fc_sp||0,
			  delayed_sale_price = data.delayed_sale_price||0;
			  if((delayed_fc_serv*1+delayed_fc_serv_up*1+delayed_fc_sp*1)>delayed_sale_price){
				  arrayBool = false;
				  util.showMsg(this, 'error', `分成不得大于售价`);
			  }
		  }

		  return arrayBool
		},
		hideMypop(){
			this.$store.state.global.eventVue.$emit('hideMypop')
		},
		sure(){
			//this.hideMypop()
			this.updateBottomPrice()
			let model;
			if(this.whProDetail.is_price_date==1){
				model = 'price_calendar'
			}else{
				model = 'only_price';
				if(!this.checkInput()){return;};
			}

			util.http.SR(this, '/index/Product/editPriceCalender', {
				[model]:JSON.stringify({
					[this.option.id]:this.productSubmitData
				})
			}, '审核成功', (rs) => {
				logic.list.refreshListData(this);
				this.hideMypop()
			});
		},
		updateBottomPrice(data){
			this.submitDateData.bottom_price = this.bottom_price;
			this.submitSingleData.bottom_price = this.submitSingleData.sale_price;
		},
        updateStockData(data) {
			//数据变化的操作
			if(Array.isArray(data)){
				//日历模式
				this.submitDateData.list = [];
				data.forEach(item=>{
					this.submitDateData.list.push({
					    ...item,
						target_date:item.date
					})
				})
			}else{
				this.submitSingleData = data
			}
        },
		getPriceCalender(id){
			util.http.getDataCom(this, '/index/Product/getPriceCalender',{
				warehouse_product_ids:id
			}, (rs) => {
				this.bottom_price = rs.bottom_price;
				this.quantityCalender = rs.quantity_calendar;
				this.priceCalendar = rs.price_calendar;
			});
		},
		getWhProDetail(id){
			let _this = this;
			util.http.getDataCom(_this, '/index/Whprod/read', {
                id: id
            }, (rs) => {
				_this.whProDetail.is_price_date = rs.is_price_date;
				util.http.getDataCom(_this, '/index/Get/get_whprod_price',{id:id},(rs)=>{
						_this.whProDetail = {..._this.whProDetail,...rs}
						//库存日历下仅能选择价格日历
						// if(this.proDetail.is_quantity_date*1===1){
						// 	this.whProDetail.is_price_date = 1
						// }
				})
            })
		},
        getProDetail(id) {
			// 	id: 14,
			// name: "默认",
			// price: "0",    //成本价
			// is_quantity_date: 0,    //是否库存日历
			// quantity: 0      //库存
            util.http.getDataCom(this, '/index/Product/read', {
                id: id
            }, (rs) => {
				this.proDetail = rs;
            })
        }
    },
    computed: {
		quantityCalenderMap(){
			let data = {};
			this.quantityCalender.forEach((item,index) =>{
				data[item.target_date] = index
			})
			return data
		},
		priceCalenderMap(){
			let data = {};
			this.priceCalendar.forEach((item,index) =>{
				data[item.target_date] = index
			})
			return data
		},
		productSubmitData(){
			if(this.whProDetail.is_price_date==1){
				return this.submitDateData
			}else{
				return this.submitSingleData
			}
		},
		productData(){
			if(this.whProDetail.is_price_date==1){
				return this.calenderData
			}else{
				return this.whProDetail
			}
		},
		calenderData(){
			let data = [];
			let dataObj = {};
			let tempalte = {
				date:'',
				sale_price:0,
				fc_serv:0,
				fc_serv_up:0,
				fc_sp:0
			}
			if(this.proDetail.is_quantity_date){
				//有库存，成本价
				//价格模板
				tempalte = {
					date:'',
					price:0,
					quantity:0,...tempalte
				}
				this.quantityCalender.forEach(item =>{
					dataObj[item.target_date] = {...tempalte,
						date:item.target_date,
						price:item.price,
						quantity:item.quantity
					}
				})
			}
			this.priceCalendar.forEach(item=>{
				let copyTem = dataObj[item.target_date]||tempalte
				dataObj[item.target_date] = {...copyTem,
					date:item.target_date,
					sale_price:item.sale_price,
					fc_serv:item.fc_serv,
					fc_serv_up:item.fc_serv_up,
					fc_sp:item.fc_sp
				}
			})
			Object.keys(dataObj).forEach(key =>{
				data.push(dataObj[key])
			})
			return data
		},
		dateShowContent(){
			if(this.proDetail.is_quantity_date){
				return this.quantityOption.concat(this.dateShowOption)
			}else{
				return this.dateShowOption
			}
		},
        componentName() {
            return this.whProDetail.is_price_date * 1 ? 'dateStock' : 'singleStock'
        }
    },
    mounted() {

    },
    props: ['option'],
    created() {
		this.getPriceCalender(this.option.id)
		this.getProDetail(this.option.data.product_id)
		this.getWhProDetail(this.option.id)
    },
    mounted() {

    }
}
</script>
<style rel="stylesheet/less" scoped lang="less">
@import "../../../common/style/mixin";
.stock-wrapper {
    .date-model {
        font-size: 18px;
    }
    .stock-action {
        text-align: center;
        margin-top: 20px;
    }
	.table-warpper{
		width: 350px;
		margin: 15px 0;
	}
	.center-box {
		text-align: center;
		width: 100%;
	}
}
</style>
